package com.hospital.pharmacy.impl;

import com.alibaba.fastjson.JSON;
import com.hospital.pharmacy.dao.*;
import com.hospital.pharmacy.entity.*;
import com.hospital.pharmacy.service.PurchaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @program: Hospital
 * @description: 进销
 * @author: 杨曼珍
 * @create: 2019-01-02 17:26
 **/

@Service
public class PurchaseServiceImpl implements PurchaseService {
    @Autowired
    PurchaseMapper purchaseMapper;
    @Autowired
    InventoryMapper inventoryMapper;
    @Autowired
    SalesRecordMapper salesRecordMapper;
    @Autowired
    MedicineMapper medicineMapper;
    @Autowired
    SuppilerMapper suppilerMapper;

//显示查找到的进货信息
    public Map selectAll(Purchase purchase){
        Map<String,String>map;
            Medicine medicine=medicineMapper.selectByPrimaryKey(purchase.getPchMdcId());
            String mdc_name=medicine.getMdcName();
            String mdc_spec=medicine.getMdcSpec();
            String mdc_unit=medicine.getMdcUnit();
            String sup_name=suppilerMapper.selectByPrimaryKey(medicine.getMdcSupplierId()).getSupName();

            map=new HashMap<>();
            map.put("id",purchase.getId().toString());
            map.put("mdcName",mdc_name);
            map.put("pchPrice",purchase.getPchPrice().toString());
            map.put("pchCount",purchase.getPchCount().toString());
            map.put("pchTotal",purchase.getPchTotal().toString());
            map.put("pchDate",new SimpleDateFormat("yyyy-MM-dd").format(purchase.getPchDate()));
            map.put("pchGuaranteeDate",new SimpleDateFormat("yyyy-MM-dd").format(purchase.getPchGuaranteeDate()));
            map.put("mdcSpec",mdc_spec);
            map.put("mdcUnit",mdc_unit);
            map.put("supName",sup_name);

        return map;
    }


//显示所有进货
    @Override
    public String selectAllPurchase(){
        List<Purchase>purchases=purchaseMapper.selectAllPurchase();
        List<Map<String,String>>resaultmap=new ArrayList<>();
        for (Purchase purchase:purchases){
            resaultmap.add(selectAll(purchase));
        }
        return JSON.toJSONString(resaultmap);

    }

    @Override
    @Transactional(propagation=Propagation.NOT_SUPPORTED)
//增加进货
    public String insertPurchase( String mec_id,Double pch_price, Integer pch_count, Double pch_total, Date pch_date, Date pch_guarantee_date, String pch_remark){
        try {

            Purchase purchase = new Purchase();
            purchase.setPchMdcId(Integer.parseInt(mec_id));
            purchase.setPchPrice(pch_price);
            purchase.setPchCount(pch_count);
            purchase.setPchTotal(pch_total);
            purchase.setPchDate(pch_date);
            purchase.setPchGuaranteeDate(pch_guarantee_date);
            purchase.setPchRemark(pch_remark);
            purchaseMapper.insertSelective(purchase);
            Inventory inventory1 = inventoryMapper.selectByMdcId(purchase.getPchMdcId());
            if (inventory1 == null) {
                Inventory inventory = new Inventory();
                inventory.setIvtrMdcId(purchase.getPchMdcId());
                inventory.setIvtrMdcDate(pch_date);
                inventory.setIvtrCount(pch_count);
                inventory.setIvtrLimit(10);
                inventory.setIvtrGuaranteeDate(pch_guarantee_date);
                inventory.setIvtrRemark(pch_remark);
                inventoryMapper.insertSelective(inventory);
            } else {
                inventory1.setIvtrMdcDate(pch_date);
                inventory1.setIvtrCount(inventory1.getIvtrCount() + pch_count);
                inventoryMapper.updateByPrimaryKey(inventory1);
            }
            System.out.println("插入成功");
            System.out.println(selectAllPurchase());
            return selectAllPurchase();
        }catch (Exception ex){
            System.out.println("插入失败");
            ex.printStackTrace();
            return JSON.toJSONString("插入失败");
        }


    }
//根据id删除
@Override
    public String deletePurchaseById(Integer id){
        try {
            Purchase purchase=purchaseMapper.selectByPrimaryKey(id);
            purchaseMapper.deleteByPrimaryKey(id);
            Inventory inventory=inventoryMapper.selectByMdcId(purchase.getPchMdcId());
            inventory.setIvtrCount(inventory.getIvtrCount()-purchase.getPchCount());
            inventoryMapper.updateByPrimaryKey(inventory);
            return selectAllPurchase();
        }catch (Exception ex){
            ex.printStackTrace();
            return JSON.toJSONString("删除失败");
        }

}
//根据药品名称查找进货信息
    @Override
    public String selectMedByName(String med_name){
            List<Medicine> medicines=medicineMapper.selectByMdcName(med_name);
            List<Map<String,String>>resaultmap=new ArrayList<>();
            for (Medicine medicine:medicines) {
                List<Purchase> purchases = purchaseMapper.selectPurchaseByMedId(medicine.getId());
                for (Purchase purchase:purchases){
                    resaultmap.add(selectAll(purchase));
                }

            }
        return JSON.toJSONString(resaultmap);

    }

//根据供货商名称查找进货信息
    @Override
    public String selectPurchaseBySupName(String sup_name){
        try {
            int sup_id = suppilerMapper.selectSuppilerBySupName(sup_name).getId();
            List<Medicine> medicines = medicineMapper.selectBySupId(sup_id);
            List<Map<String,String>>resaultmap=new ArrayList<>();
            for (Medicine medicine : medicines) {
                List<Purchase> purchases = purchaseMapper.selectPurchaseByMedId(medicine.getId());
                for (Purchase purchase:purchases){
                    resaultmap.add(selectAll(purchase));
                }
            }
            return JSON.toJSONString(resaultmap);
        }catch (Exception e){
            return JSON.toJSONString("失败");
        }
    }

    //根据id查找purchase
    @Override
    public Purchase selectPurchaseById(int purchase_id){

            Purchase purchase=purchaseMapper.selectByPrimaryKey(purchase_id);
            return purchase;

    }


    //根据 id修改进货信息
    @Override
    public String updatePurchaseByPurchase(int pch_Id,double pchPrice,double pchTotal,int pchCount,Date pchDate,Date pchGuaranteeDate){
        try {
            Purchase purchase=purchaseMapper.selectByPrimaryKey(pch_Id);
            Inventory inventory=inventoryMapper.selectByMdcId(purchase.getPchMdcId());
            if (purchase.getPchCount()-pchCount<0) {
                inventory.setIvtrCount(inventory.getIvtrCount() + (pchCount - purchase.getPchCount()));
            }else {
                inventory.setIvtrCount(inventory.getIvtrCount()-(purchase.getPchCount()-pchCount));
            }
            purchase.setPchPrice(pchPrice);
            purchase.setPchTotal(pchTotal);
            purchase.setPchDate(pchDate);
            purchase.setPchGuaranteeDate(pchGuaranteeDate);
            purchase.setPchCount(pchCount);
            inventoryMapper.updateByPrimaryKey(inventory);
            purchaseMapper.updateByPrimaryKeySelective(purchase);
            return selectAllPurchase();
        }catch(Exception e){
            e.printStackTrace();
            return JSON.toJSONString("失败");
        }

    }
    //根据日期查找进货记录
    @Override
    public String selectPurchaseByDate(Date date){
        try {
            List<Purchase>purchases=purchaseMapper.selectPurchaseByDate(date);
            List<Map<String,String>>resaultmap=new ArrayList<>();
            for (Purchase purchase:purchases){
                resaultmap.add(selectAll(purchase));
            }
            return JSON.toJSONString(resaultmap);
        }catch (Exception e){
            e.printStackTrace();
            return JSON.toJSONString("失败");
        }
    }





/*List<Map<String,String>>resaultmap=new ArrayList<>();
        Map<String,String>map;
        for (Medicine medicine:medicines){
            map=new HashMap<>();
            map.put("id",medicine.getId().toString());
            map.put("mdc_num",medicine.getId().toString());
            map.put("mdc_name",medicine.getMdcName());
            map.put("mdc_unit",medicine.getMdcUnit());
            map.put("mdc_spec",medicine.getMdcSpec());
            map.put("mdc_type_id",medicine.getMdcTypeId().toString());
            map.put("mdc_supplier_id",medicine.getMdcSupplierId().toString());
            map.put("mdc_company",medicine.getMdcCompany());
            map.put("mdc_remark",medicine.getMdcRemark());
            resaultmap.add(map);
        }*/


/*    public String selectAll(List<Purchase>purchases){
        List<Map<String,String>>resaultmap=new ArrayList<>();
        Map<String,String>map;
        for (Purchase purchase:purchases){
            Medicine medicine=medicineMapper.selectByPrimaryKey(purchase.getPchMdcId());
            String mdc_name=medicine.getMdcName();
            String mdc_spec=medicine.getMdcSpec();
            String mdc_unit=medicine.getMdcUnit();
            String sup_name=suppilerMapper.selectByPrimaryKey(medicine.getMdcSupplierId()).getSupName();

            map=new HashMap<>();
            map.put("id",purchase.getId().toString());
            map.put("mdcName",mdc_name);
            map.put("pchPrice",purchase.getPchPrice().toString());
            map.put("pchCount",purchase.getPchCount().toString());
            map.put("pchTotal",purchase.getPchTotal().toString());
            map.put("pchDate",new SimpleDateFormat("yyyy-MM-dd").format(purchase.getPchDate()));
            map.put("pchGuaranteeDate",new SimpleDateFormat("yyyy-MM-dd").format(purchase.getPchGuaranteeDate()));
            map.put("mdcSpec",mdc_spec);
            map.put("mdcUnit",mdc_unit);
            map.put("supName",sup_name);
            resaultmap.add(map);
        }
        return JSON.toJSONString(resaultmap);
    }*/
}
